﻿Friend Class RegexProject
    Inherits ModelBase
    Implements IRegexProject

#Region "Events"
    ' Insert Event Declarations Here
#End Region

#Region "Properties"
#Region "Constants"
    ' Insert Constant Declarations Here
#End Region

    Private _directoryInput As IDirectoryInput
    Private ReadOnly Property DirectoryInput As IDirectoryInput Implements IRegexProject.DirectoryInput
        Get
            Return _directoryInput
        End Get
    End Property

    Private _literalTextInput As ILiteralTextInput
    Private ReadOnly Property LiteralTextInput As ILiteralTextInput Implements IRegexProject.LiteralTextInput
        Get
            Return _literalTextInput
        End Get
    End Property

    Private _multipleFilesInput As IMultipleFilesInput
    Private ReadOnly Property MultipleFilesInput As IMultipleFilesInput Implements IRegexProject.MultipleFilesInput
        Get
            Return _multipleFilesInput
        End Get
    End Property

    Private _registryInput As IRegistryInput
    Private ReadOnly Property RegistryInput As IRegistryInput Implements IRegexProject.RegistryInput
        Get
            Return _registryInput
        End Get
    End Property

    Private _regexHistory As New Stack(Of Tuple(Of String, RegexOptions))
    Private ReadOnly Property RegexHistory As Stack(Of Tuple(Of String, RegexOptions)) Implements IRegexProject.RegexHistory
        Get
            If _regexHistory IsNot Nothing Then
                ' Return a copy so that the original history cannot be tampered with
                Return New Stack(Of Tuple(Of String, RegexOptions))(_regexHistory.Reverse())
            Else
                Return Nothing
            End If
        End Get
    End Property
#End Region

#Region "Constructors"
    Private Sub New()
        _directoryInput = New DirectoryInput()
        _literalTextInput = New LiteralTextInput
        _multipleFilesInput = New MultipleFilesInput()
        _registryInput = New RegistryInput()
    End Sub
#End Region

#Region "Methods"
    Public Shared Function CreateNew() As IRegexProject
        Return New RegexProject()
    End Function

    Public Shared Function Open(stream As IO.Stream) As IRegexProject
        Throw New NotImplementedException()
    End Function

    Public Function FormatRegex(formatter As IRegexFormatter) As String Implements IRegexProject.FormatRegex
        Dim l_lastRegex = Me.RegexHistory.Peek()
        Return formatter.Format(l_lastRegex.Item1, l_lastRegex.Item2)
    End Function

    Private Sub RecordRegex(pattern As String, options As System.Text.RegularExpressions.RegexOptions) Implements IRegexProject.RecordRegex
        Dim l_previousEntry = If(_regexHistory.Count > 0, _regexHistory.Peek(), Nothing)
        If (
            l_previousEntry Is Nothing OrElse
            l_previousEntry.Item1 <> pattern OrElse
            l_previousEntry.Item2 <> options
        ) Then
            _regexHistory.Push(New Tuple(Of String, RegexOptions)(pattern, options))
            OnPropertyChanged("RegexHistory")
        End If
    End Sub

#Region "Event Raiser Methods"
    ' Insert Method Declarations which Raise an Event Here
#End Region

#Region "Event Handlers"
    ' Insert Method Declarations which Handle an Event Here
#End Region
#End Region

End Class
